package com.rjc.boot.python;

import org.junit.Test;
import org.python.modules._codecs;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Time;
import java.util.Timer;

@Component
public class DetermineCode {
    @Value("${temporary.file}")
    public String FILE;
    @Value("${temporary.dir}")
    public String DIR;
    //    @Test
    public String getAns(String code, String input) {

        setFile(code);
        Process proc;
        StringBuilder sb = new StringBuilder();
        try {

            String cmd1 = "python" + " "  + DIR+ FILE;
            proc = Runtime.getRuntime().exec(cmd1);
            //写入代码依赖的输入
            if(!input.equals("")){
                OutputStream outputStream = proc.getOutputStream();
                outputStream.write(input.getBytes("UTF-8"));
                outputStream.close();
            }

            //用输出流来截取结果
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"UTF-8"));
            String line = null;
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
            in.close();

            //输出流长度为零，即程序运行错误，获取错误流
            if(sb.length() == 0){
                in = new BufferedReader(new InputStreamReader(proc.getErrorStream(),"UTF-8"));
                line = null;
                while ((line = in.readLine()) != null) {
                    sb.append(line);
                }
                in.close();
            }
            proc.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }
    public void setFile(String code){
        createDir(null);
        Writer writer = null;
        try {
            File file = new File(DIR+FILE);
            if (!file.exists()) {
                boolean newFile = file.createNewFile();
            }

            writer = new FileWriter(DIR+FILE );
            writer.write(code); // 写入String
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /***
     * 创建临时文件，递归生成目录
     */
    public void createDir(String dir){
        if(dir==null) dir = DIR;
        File file = new File(dir);

        String substring = dir.substring(dir.length() - 2, dir.length());
        boolean equals = substring.equals(":\\");
        if(equals){
            return;
        }
        if(!file.exists()){
            createDir(file.getParent());
        }
        file.mkdir();




//        System.out.println(file.getAbsolutePath());
//        System.out.println(file.getParent());
//        System.out.println(file.getParentFile().getParent());
//        System.out.println(file.getParentFile().getParentFile().getParent());
//        D:\temporary\0\temporary.py
//        D:\temporary\0
//        D:\temporary
//        D:\


    }


}
